const { DataTypes } = require("sequelize");
const { sequelize } = require("../dbConnect");

// 文件上传记录表
const FileUpload = sequelize.define('file_uploads', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: "文件上传ID"
  },
  user_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: "上传用户ID"
  },
  original_name: {
    type: DataTypes.STRING(255),
    allowNull: false,
    comment: "原始文件名"
  },
  filename: {
    type: DataTypes.STRING(255),
    allowNull: false,
    comment: "存储文件名"
  },
  file_path: {
    type: DataTypes.STRING(500),
    allowNull: false,
    comment: "文件路径"
  },
  file_url: {
    type: DataTypes.STRING(500),
    allowNull: false,
    comment: "文件访问URL"
  },
  file_type: {
    type: DataTypes.ENUM('audio', 'video', 'image', 'document'),
    allowNull: false,
    comment: "文件类型"
  },
  mime_type: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: "MIME类型"
  },
  file_size: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: "文件大小(字节)"
  },
  duration: {
    type: DataTypes.FLOAT,
    allowNull: true,
    comment: "媒体文件时长(秒)"
  },
  thumbnail: {
    type: DataTypes.STRING(500),
    allowNull: true,
    comment: "缩略图URL"
  },
  usage_type: {
    type: DataTypes.ENUM('detection', 'game', 'daily', 'course', 'avatar', 'other'),
    allowNull: false,
    comment: "使用类型"
  },
  related_id: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: "关联记录ID"
  },
  status: {
    type: DataTypes.ENUM('uploading', 'completed', 'failed', 'deleted'),
    defaultValue: 'uploading',
    comment: "上传状态"
  }
}, {
  tableName: 'file_uploads',
  indexes: [
    {
      fields: ['user_id']
    },
    {
      fields: ['user_id', 'usage_type']
    }
  ]
});

// 用户学习统计表
const UserStudyStatistics = sequelize.define('user_study_statistics', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: "统计ID"
  },
  user_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    unique: true,
    comment: "用户ID"
  },
  total_study_time: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "总学习时长(秒)"
  },
  total_practices: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "总练习次数"
  },
  average_score: {
    type: DataTypes.DECIMAL(5, 2),
    defaultValue: 0.00,
    comment: "平均分数"
  },
  best_score: {
    type: DataTypes.DECIMAL(5, 2),
    defaultValue: 0.00,
    comment: "最高分数"
  },
  continuous_days: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "连续学习天数"
  },
  total_days: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "总学习天数"
  },
  last_study_date: {
    type: DataTypes.DATEONLY,
    allowNull: true,
    comment: "最后学习日期"
  },
  weekly_data: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: "周统计数据"
  },
  monthly_data: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: "月统计数据"
  },
  score_distribution: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: "分数分布"
  },
  improvements: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: "进步数据"
  }
}, {
  tableName: 'user_study_statistics',
  indexes: [
    {
      unique: true,
      fields: ['user_id']
    }
  ]
});

module.exports = {
  FileUpload,
  UserStudyStatistics
};
